package com.tledu.web_01;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/resp/test/ttt")
public class Response01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        // 设置响应的状态码
//        resp.setStatus(302);
//        // 设置相应头
//        resp.setHeader("Location","http://www.baidu.com");
//        resp.sendRedirect("http://www.baidu.com");
        // 绝对路径，相对于 协议://ip:port 它是不会带上项目的前缀的
//        resp.sendRedirect(req.getContextPath()+"/stu/manager");
        // 相对路径 http://localhost:8085/web_01_war_exploded/resp/test/stu/manager  错
        // 相对路径 http://localhost:8085/web_01_war_exploded/resp/stu/manager 对
        // 去掉最后一个斜杠后面的内容，拼上对应地址
        resp.sendRedirect("../../stu/manager");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "张三");

        resp.getWriter().println(jsonObject.toJSONString());
    }
}
